//
//  MBDMessageCell.swift
//  MasterBoy
//
//  Created by kyson on 2023/11/7.
//

import Foundation
import UIKit
import SnapKit
import YYText


class MBDMessageCell : MBDCommonCell {
    
    private var titleLabel : YYLabel!
    private var subtitleLabel : YYLabel!

    private var messageCoverImageView : UIImageView!
    
    private var separaterLine : UIView!
        
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //暂不支持暗黑模式，所有背景颜色都写死
        self.contentView.backgroundColor = UIColor.white
        
        self.initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        self.initialize()
    }
    
    func initialize() {
        self.contentView.setDynamicForgroundColor()
        self.commonBackgroundView.setDynamicForgroundColor()
        
        titleLabel = YYLabel()
        self.contentView.addSubview(titleLabel)
        
        subtitleLabel = YYLabel()
        self.contentView.addSubview(subtitleLabel)
        
        separaterLine = UIView()
        self.contentView.addSubview(separaterLine)
        separaterLine.setDynamicSeperateLineColor()
        
        messageCoverImageView = UIImageView()
        messageCoverImageView.layer.cornerRadius = 18
        messageCoverImageView.clipsToBounds = true
        self.contentView.addSubview(messageCoverImageView)
        
        messageCoverImageView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.top.equalTo(8)
            make.width.equalTo(36)
            make.height.equalTo(36)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(messageCoverImageView.snp.right).offset(8)
            make.top.equalTo(8)
            make.width.equalTo(0)
            make.height.equalTo(0)
        }
        
        subtitleLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
            make.right.equalTo(-MBDMainCellModel.marginSubtitleRight)
            make.height.equalTo(0)
        }
        
    }
    
    deinit {
        print("MBDGroupMainCell was deallocated")
    }
    
    override var cellModel: Any! {
        didSet {
            let realCellModel = cellModel as! MBDMessageCellModel
            
            self.titleLabel.textLayout = realCellModel.titlelayout

            titleLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.titlelayout!.textBoundingSize.height)
                make.width.equalTo(realCellModel.titlelayout!.textBoundingSize.width)
            }
          
            self.subtitleLabel.textLayout = realCellModel.subtitlelayout

            subtitleLabel.snp.updateConstraints { make in
                make.height.equalTo(realCellModel.subtitlelayout!.textBoundingSize.height)
            }
            
            if let coverURL = realCellModel.coverURL {
                
                
                if coverURL.hasPrefix(MBDPath2URLUtils.filePathPrefix) {
                    let imageName = coverURL.replacingOccurrences(of: MBDPath2URLUtils.filePathPrefix, with: "")
                    
                    messageCoverImageView.image = UIImage(named: imageName)
                } else {
                    let url = URL.init(string: MBDPath2URLUtils.wrapAvatarURL(path: coverURL))
                    messageCoverImageView.kf.setImage(with: url)
                }
                
                messageCoverImageView.snp.updateConstraints { make in
                    make.height.equalTo(36)
                }
            } else {
                debugPrint("----")
            }
            
            
                        
        }
    }
    
}
